package com.ww.www;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.Dimension;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeClass;

import io.appium.java_client.MobileElement;
import io.appium.java_client.android.Activity;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.android.AndroidTouchAction;
import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.touch.LongPressOptions;
import io.appium.java_client.touch.offset.ElementOption;

public class BaseTest {
	String urlStr = "http://127.0.0.1:4723/wd/hub";
	Dimension dimension=null;
	AndroidDriver<AndroidElement> driver = null;

	@BeforeClass
	public void initDirver() throws MalformedURLException {
		DesiredCapabilities cap = new DesiredCapabilities();
		File app = new File("./apps/todolis.apk");
		System.out.println(app.getAbsolutePath());
//		�������Ҫ��װapp���������ע�͵�
//		cap.setCapability("app", app.getAbsolutePath());
		cap.setCapability("platformName", "Android");
		cap.setCapability("deviceName", "127.0.0.1:62001");
		
		cap.setCapability("browserName", "Chrome");
//		 com.android.browser/.BrowserActivity
		
//		cap.setCapability("appPackage", "io.appium.android.apis");
//		cap.setCapability("appActivity",".ApiDemos");
		
//		cap.setCapability("appPackage", "com.mobeta.android.demodslv");
//		cap.setCapability("appActivity", ".Launcher");
		
//		cap.setCapability("appPackage", "com.example.todolist");
//		cap.setCapability("appActivity", ".LoginActivity");
		
		cap.setCapability("noReset", false);
//		cap.setCapability(MobileCapabilityType.APP, ".LoginActivity");
//		driver.startActivity(new Activity("com.example.todolist",
//				".LoginActivity"));

		URL url = new URL(urlStr);

		driver = new AndroidDriver<AndroidElement>(url, cap);
	
		dimension=driver.manage().window().getSize();
	
	}

	public void longPress(MobileElement item) {
		AndroidTouchAction action = new AndroidTouchAction(driver);
		LongPressOptions longPress = LongPressOptions.longPressOptions();
		longPress.withElement(ElementOption.element(item));
		action.longPress(longPress).release().perform();
	}
	
	public void drag(AndroidElement from,AndroidElement to) {
	
		AndroidTouchAction action=new AndroidTouchAction(driver);
		
		LongPressOptions longPress = LongPressOptions.longPressOptions();
		longPress.withElement(ElementOption.element(from));
		
		action.longPress(longPress)
		.moveTo(ElementOption.element(to))
		.release().perform();
	}
}
